﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Data.SqlClient;

public partial class frmListaDeEmpresas : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmEmpresa.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        /*Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=ListaEmpresasPDF.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GridView1.AllowPaging = true;
        GridView1.DataBind();
        GridView1.RenderControl(htmlTextWriter);
        GridView1.HeaderRow.Style.Add("width", "10%");
        GridView1.HeaderRow.Style.Add("font-size", "8px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "8px");

        StringReader sr = new StringReader(stringWriter.ToString());
        Document doc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
        doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
        HTMLWorker htmlparser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        htmlparser.Parse(sr);
        doc.Close();
        Response.Write(doc);
        Response.End();*/
    }

   public override void VerifyRenderingInServerForm (Control control)
{

}

  
   protected void Button3_Click1(object sender, EventArgs e)
   {
       String fecha = DateTime.Now.ToString("dd/MM/yyyy");
       string imagepath = Server.MapPath("Img");

       this.SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;
       SqlDataReader datos = (SqlDataReader)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
       iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
       iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
       iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
      
       FileStream fs = new FileStream(Server.MapPath("~/") + "listaempresas.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
       Rectangle rec3 = new Rectangle(PageSize.A4.Rotate());
       Document doc = new Document(rec3, 5, 5, 5, 5);
       PdfWriter writer = PdfWriter.GetInstance(doc, fs);

       Console.WriteLine("");

       Paragraph titulo = new Paragraph("LISTADO DE EMPRESAS AL " + fecha, fontTitle);
       titulo.Alignment = Element.ALIGN_JUSTIFIED;

       doc.Open();
       iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
       doc.Add(logo);
       doc.Add(new Paragraph("  "));
       doc.Add(titulo);
       doc.Add(new Paragraph("  "));
       iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(12);
       table.HorizontalAlignment = 0;
       table.TotalWidth = 792f;
       table.LockedWidth = true;
       float[] widths = new float[] { 54f, 110f, 110f, 88f, 60f, 110f, 30f, 30f, 30f, 70f, 60f, 50f };
       table.SetWidths(widths);

       PdfPCell hcuit = new PdfPCell(new Phrase("CUIT", new Font(fontHead)));
       PdfPCell hrazon = new PdfPCell(new Phrase("RAZON SOCIAL", new Font(fontHead)));
       PdfPCell hcomercial = new PdfPCell(new Phrase("NOMBRE COMERCIAL", new Font(fontHead)));
       PdfPCell hmail = new PdfPCell(new Phrase("E-MAIL", new Font(fontHead)));
       PdfPCell htelefono = new PdfPCell(new Phrase("TELEFONO", new Font(fontHead)));
       PdfPCell hcalle = new PdfPCell(new Phrase("CALLE", new Font(fontHead)));
       PdfPCell hnro = new PdfPCell(new Phrase("NRO.", new Font(fontHead)));
       PdfPCell hpiso = new PdfPCell(new Phrase("PISO", new Font(fontHead)));
       PdfPCell hdpto = new PdfPCell(new Phrase("DPTO.", new Font(fontHead)));
       PdfPCell hbarrio = new PdfPCell(new Phrase("BARRIO", new Font(fontHead)));
       PdfPCell hlocalidad = new PdfPCell(new Phrase("LOCALIDAD", new Font(fontHead)));
       PdfPCell hestado = new PdfPCell(new Phrase("ESTADO", new Font(fontHead)));

       table.AddCell(hcuit);
       table.AddCell(hrazon);
       table.AddCell(hcomercial);
       table.AddCell(hmail);
       table.AddCell(htelefono);
       table.AddCell(hcalle);
       table.AddCell(hnro);
       table.AddCell(hpiso);
       table.AddCell(hdpto);
       table.AddCell(hbarrio);
       table.AddCell(hlocalidad);
       table.AddCell(hestado);

       doc.Add(new Paragraph("  "));
       while (datos.Read())
       {
           String cuit = datos["cuit"].ToString();
           String razon = datos["razonSocial"].ToString();
           String comercial = datos["nombreComercial"].ToString();
           String mail = datos["dirCorreo"].ToString();
           String telefono = datos["numTelefono"].ToString();
           String calle = datos["calle"].ToString();
           String numero = datos["numeroCalle"].ToString();
           String piso = datos["piso"].ToString();
           String dpto = datos["departamento"].ToString();
           String barrio = datos["nombreBarrio"].ToString();
           String localidad = datos["nombreLocalidad"].ToString();
           String estado = datos["estado"].ToString();

           PdfPCell ccuit = new PdfPCell(new Phrase(cuit, new Font(fontBody)));
           PdfPCell crazon = new PdfPCell(new Phrase(razon, new Font(fontBody)));
           PdfPCell ccomercial = new PdfPCell(new Phrase(comercial, new Font(fontBody)));
           PdfPCell cmail = new PdfPCell(new Phrase(mail, new Font(fontBody)));
           PdfPCell ctelefono = new PdfPCell(new Phrase(telefono, new Font(fontBody)));
           PdfPCell ccalle = new PdfPCell(new Phrase(calle, new Font(fontBody)));
           PdfPCell cnumero = new PdfPCell(new Phrase(numero, new Font(fontBody)));
           PdfPCell cpiso = new PdfPCell(new Phrase(piso, new Font(fontBody)));
           PdfPCell cdpto = new PdfPCell(new Phrase(dpto, new Font(fontBody)));
           PdfPCell cbarrio = new PdfPCell(new Phrase(barrio, new Font(fontBody)));
           PdfPCell clocalidad = new PdfPCell(new Phrase(localidad, new Font(fontBody)));
           PdfPCell cestado = new PdfPCell(new Phrase(estado, new Font(fontBody)));


           table.AddCell(ccuit);
           table.AddCell(crazon);
           table.AddCell(ccomercial);
           table.AddCell(cmail);
           table.AddCell(ctelefono);
           table.AddCell(ccalle);
           table.AddCell(cnumero);
           table.AddCell(cpiso);
           table.AddCell(cdpto);
           table.AddCell(cbarrio);
           table.AddCell(clocalidad);
           table.AddCell(cestado);



       }
       doc.Add(table);
       doc.Add(new Paragraph("  "));
       doc.Add(new Paragraph("  "));

       doc.Close();

       Response.ContentType = "application/pdf";
       Response.AddHeader("Content-Disposition", "attachment;filename=listaempresas.pdf");
       Response.TransmitFile(Server.MapPath("~/") + "listaempresas.pdf");

   }
   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {
       Session ["cuit"] = this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[1].Text;
       Response.Redirect("frmEmpresa.aspx");
   }
}